﻿using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace rabbitmqdemo.Controllers
{
    [Route("api/test")]
    public partial class RabbitmqTestController : Controller
    {
        private ILogger<RabbitmqTestController> _logger;

        public RabbitmqTestController(ILogger<RabbitmqTestController> logger)
        {
            _logger = logger;
        }

        private string queueName = "hello";
        private string exchangeName = "";
        private string routingKey = "routingKey";

        [HttpGet]
        [Route("send")]
        public Object Send()
        {
            ConnectionFactory factory = new ConnectionFactory()
            {
                UserName = "root",
                Password = "root",
                HostName = "192.168.3.34"
            };

            //创建连接
            var connection = factory.CreateConnection();
//创建通道
            var channel = connection.CreateModel();
            
//声明一个队列
            channel.QueueDeclare("hello", false, false, false, null);

            Console.WriteLine("\nRabbitMQ连接成功，请输入消息，输入exit退出！");

            string input;
            for (int i = 0; i < 1000; i++)
            {
                input = "生成文档" + Guid.NewGuid();

                var sendBytes = Encoding.UTF8.GetBytes(input);
                //发布消息
                channel.BasicPublish("", "hello", null, sendBytes);
            }

            channel.Close();
            connection.Close();

            return "write data to mq";
        }

        [HttpGet]
        [Route("get")]
        public Object Get()
        {
            ConnectionFactory factory = new ConnectionFactory()
            {
                UserName = "root",
                Password = "root",
                HostName = "192.168.3.34"
            };

            Parallel.For(0, 10, (i) =>
            {
                i++;
                //创建连接
                var connection = factory.CreateConnection();
                //创建通道
                var channel = connection.CreateModel();

                var randomName = "消费者" + Guid.NewGuid();
                //事件基本消费者
                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

                //接收到消息事件
                consumer.Received += (ch, ea) =>
                {
                    var message = Encoding.UTF8.GetString(ea.Body);
                    Console.WriteLine($"{DateTime.Now} {randomName}收到消息： {message}");
                    Thread.Sleep(50);
                    //确认该消息已被消费
                    channel.BasicAck(ea.DeliveryTag, false);
                };
                //启动消费者 设置为手动应答消息
                channel.BasicConsume("hello", false, consumer);
                Console.WriteLine("消费者已启动");
                Console.ReadKey();


                channel.Dispose();
                connection.Close();
            });
            return "read data from mq";
        }
    }
}